<?php

function create_pages($data, $params, $show_count = false, $num_input = false)
{
    if($data['totalCount']<=0)
    {
    	return " ";
    }
	//初始化
    $firstPage = 1;
    $slider = $data['slider'] <> '' ? $data['slider'] : 1;
    $length = $data['length'] <> '' ? $data['length'] : 6;
    //$firstLabel = '<font face="webdings">9</font>';
    //$lastLabel = '<font face="webdings">:</font>';
    //$prevLabel = '<font face="webdings">3</font>';
    //$nextLabel = '<font face="webdings">4</font>';
	
	$firstLabel = '|<';
    $lastLabel = '>|';
    $prevLabel = '<';
    $nextLabel = '>';
	
    //每页显示条数
    $pageSize = $data['pageSize'] > 0 ? $data['pageSize'] : 20;
    //总页数
    $pageCount 	= ceil($data['totalCount'] / $pageSize);
    $lastPage = $pageCount;
    //上一页
    $prePage 	= $data['currentPage'] > 1 ? $data['currentPage']-1 : 1;
    //下一页
    $nextPage 	= $data['currentPage'] < $pageCount ? $data['currentPage']+1 : $pageCount;
    
    //当前页
    $currentPage = isset($data['currentPage']) && $data['currentPage']>0 ? $data['currentPage'] : 1;

    $output = '';
    if ($show_count) {
    	$output .= "<div class=\"page_info\">共".$data['totalCount']."条(".$pageCount."页),第".(($data['currentPage']-1)*$data['pageSize']+1)."—".($data['currentPage']*$data['pageSize'])."条</div>";
    }
    
    $output .= "<div class=\"page_btn\">\n";
    
    //首页
    if ($currentPage <> $firstPage) {
        $url = page_url($params, 1);
        $output .= "<a href=\"{$url}\" title=\"首页\">{$firstLabel}</a>\n";
    }
    //上一页
    if ($currentPage <> $firstPage) {
        $url = page_url($params, $currentPage-1);
        $output .= "<a href=\"{$url}\" title=\"上一页\">{$prevLabel}</a>\n";
    }
    
    //连续页码开始
    $mid = intval($length / 2);
    if ($currentPage < $firstPage) {
        $currentPage = $firstPage;
    }
    if ($currentPage > $lastPage) {
        $currentPage = $lastPage;
    }
	//中间连续数字的开始页码
    $begin = $currentPage - $mid;
    if ($begin < $firstPage) 
    {
    	$begin = $firstPage; 
    }
    //中间连续数字的结束页码
    $end = $begin + $length - 1;
    
    if ($end >= $lastPage) {
        $end = $lastPage;
        $begin = $end - $length + 1;
        if ($begin < $firstPage) { 
        	$begin = $firstPage; 
        }
    }

    if ($begin > $firstPage) {
        for ($i = $firstPage; $i < $firstPage + $slider && $i < $begin; $i++) {
            //$in = $i + 1;
            $url = page_url($params, $i);
            $output .= "<a href=\"{$url}\">{$i}</a>\n";
        }

        if ($i < $begin) {
            $output .= "<span class=\"page_dot\">...</span>\n";
        }
    }

    for ($i = $begin; $i <= $end; $i++) {
        //$in = $i + 1;
        if ($i == $data['currentPage']) {
            $output .= "<a class=\"curPage\">{$i}</a>\n";
        } else {
            $url = page_url($params, $i);
            $output .= "<a href=\"{$url}\">{$i}</a>\n";
        }
    }

    if ($lastPage - $end > $slider) {
        $output .= "<span class=\"page_dot\">...</span>\n";
        $end = $lastPage - $slider;
    }

    for ($i = $end + 1; $i <= $lastPage; $i++) {
        //$in = $i + 1;
        $url = page_url($params, $i);
        $output .= "<a href=\"{$url}\">{$i}</a>\n";
    }
    //连续页码结束
    
	//下一页
    if ($currentPage <> $lastPage) {
        $url = page_url($params, $nextPage);
        $output .= "<a href=\"{$url}\" title=\"下一页\">{$nextLabel}</a>\n";
    }
    //末页
    
    if ($currentPage <> $lastPage) {
        $url = page_url($params, $lastPage);
        $output .= "<a href=\"{$url}\" title=\"尾页\">{$lastLabel}</a>\n";
    }
	
	//显示直接输入页数跳转
	if ($num_input) {
		$gets = $params;
		$url = page_url($gets, $currentPage, true);
    	$output .= "<div class=\"page_jump\"><form name=\"page_form999\" method=\"get\" action=\"".$url."\" id=\"page_form1\">到第<input name=\"page\" id=\"phpPage1_input\" type=\"text\" value=\"".$currentPage."\" style=\"width:30px;\">页<input type=\"submit\" value=\"确定\" name=\"pgbtn\" id=\"phpPage_btn\">";
		//整理当前的get参数
		unset($gets['page']);
		unset($gets['button']);
		unset($gets['phpPage_btn']);
		$gets = array_unique($gets);
		remove_empty_params($gets);
		if(!empty($gets))
		{
			foreach($gets AS $key => $val)
			{
				$output .= "<input name=\"".$key."\" type=\"hidden\" value=\"".$val."\">";
			}
		}
		$output .= "</form></div>";
    }
	
    $output .= "</div>\n";
	//右侧结尾

	return $output;
}

function create_pages_new($data, $params, $show_count = false, $num_input = false)
{
    if($data['totalCount']<=0)
    {
    	return " ";
    }
	//初始化
    $firstPage = 1;
    $slider = $data['slider'] <> '' ? $data['slider'] : 1;
    $length = $data['length'] <> '' ? $data['length'] : 6;
    //$firstLabel = '<font face="webdings">9</font>';
    //$lastLabel = '<font face="webdings">:</font>';
    //$prevLabel = '<font face="webdings">3</font>';
    //$nextLabel = '<font face="webdings">4</font>';
	
	$firstLabel = '|<';
    $lastLabel = '>|';
    $prevLabel = '<';
    $nextLabel = '>';
	
    //每页显示条数
    $pageSize = $data['pageSize'] > 0 ? $data['pageSize'] : 20;
    //总页数
    $pageCount 	= ceil($data['totalCount'] / $pageSize);
    $lastPage = $pageCount;
    //上一页
    $prePage 	= $data['currentPage'] > 1 ? $data['currentPage']-1 : 1;
    //下一页
    $nextPage 	= $data['currentPage'] < $pageCount ? $data['currentPage']+1 : $pageCount;
    
    //当前页
    $currentPage = isset($data['currentPage']) && $data['currentPage']>0 ? $data['currentPage'] : 1;

    $output = '';
    
                            
    //首页
    if ($currentPage <> $firstPage) {
        $url = page_url($params, 1);
        $output .= "<a href='$url' class='one_end'>首页</a>";
    }
    //上一页
    if ($currentPage <> $firstPage) {
        $url = page_url($params, $currentPage-1);
        $output .= "<span class='disabled_l'><a href='$url'  style='width: 100%;border: none;padding: 0;margin: 0;'>上一页</a></span>";
    }
    
    //连续页码开始
    $mid = intval($length / 2);
    if ($currentPage < $firstPage) {
        $currentPage = $firstPage;
    }
    if ($currentPage > $lastPage) {
        $currentPage = $lastPage;
    }
	//中间连续数字的开始页码
    $begin = $currentPage - $mid;
    if ($begin < $firstPage) 
    {
    	$begin = $firstPage; 
    }
    //中间连续数字的结束页码
    $end = $begin + $length - 1;
    
    if ($end >= $lastPage) {
        $end = $lastPage;
        $begin = $end - $length + 1;
        if ($begin < $firstPage) { 
        	$begin = $firstPage; 
        }
    }

    if ($begin > $firstPage) {
        for ($i = $firstPage; $i < $firstPage + $slider && $i < $begin; $i++) {
            //$in = $i + 1;
            $url = page_url($params, $i);
            if($i == $data['currentPage']){
                $output .= "<span class='current'><a href='$url'style='width: 100%;border: none;padding: 0;margin: 0;'>$i</a></span>";
            }else{
                $output .= "<a href='$url' class='tcdNumber'>$i</a>";
            }
        }

        if ($i < $begin) {
            $output .= "<span class='pageCount'>...</span>";
        }
    }

    for ($i = $begin; $i <= $end; $i++) {
        //$in = $i + 1;
        if ($i == $data['currentPage']) {
            $output .= "<span class='current'><a href='$url'style='width: 100%;border: none;padding: 0;margin: 0;'>$i</a></span>";
        } else {
            $url = page_url($params, $i);
            $output .= "<a href='$url' class='tcdNumber'>$i</a>";
        }
    }

    if ($lastPage - $end > $slider) {
        $output .= "<span class='pageCount'>...</span>";
        $end = $lastPage - $slider;
    }

    for ($i = $end + 1; $i <= $lastPage; $i++) {
        //$in = $i + 1;
        $url = page_url($params, $i);
        $output .= "<a href='$url' class='tcdNumber'>$i</a>";
    }
    //连续页码结束
    
	//下一页
    if ($currentPage <> $lastPage) {
        $url = page_url($params, $nextPage);
        $output .= "<a href='$url' class='nextPage'>下一页</a>";
    }
    //末页
    
    if ($currentPage <> $lastPage) {
        $url = page_url($params, $lastPage);
        $output .= "<a href='$url' class='one_end'>尾页</a>";
    }

	return $output;
}

function page_url($params, $page, $form=false)
{
	$page_name = 'page';
	$url = '';
	$url .= PHP_SELF;
	
	unset($params[$page_name]);
	unset($params['button']);
	unset($params['phpPage_btn']);
	remove_empty_params($params);
	if(!empty($params))
	{
		foreach($params AS $key => $val)
		{
			$url .= ((stristr($url,'?'))?'&':'?') . $key . "=" . $val;
		}
		if(!$form)
		{
			$url .= "&".$page_name."=".$page;
		}
	}
	else 
	{
		if(!$form)
		{
			$url .= "?".$page_name."=".$page;
		}
	}
	
	return $url;
}

function remove_empty_params(& $arr, $trim = true)
{
    foreach ($arr as $key => $value) {
        if (is_array($value)) {
            array_remove_empty($arr[$key]);
        } else {
            $value = trim($value);
            if ($value == '') {
                unset($arr[$key]);
            } elseif ($trim) {
                $arr[$key] = $value;
            }
        }
    }
}